// Page({
//   data: {
//     target: [{
//       schoolIndex: 0,
//       majorIndex: 0
//     }]
//   },
//   onAddTarget() {
//     this.data.target.push({
//       schoolIndex: 0,
//       majorIndex: 0
//     });
//     this.setData({
//       target: this.data.target
//     });
//   },
//   onSchoolChange(e) {
//     console.log('School changed in branch', e.currentTarget.dataset.idx, e.detail.schoolIndex);
//     const index = e.currentTarget.dataset.idx;
//     const value = e.detail.schoolIndex;
//     this.setData({
//       [`target[${index}].schoolIndex`]: value
//     });
//   },
//   onMajorChange(e) {
//     console.log('Major changed in branch', e.currentTarget.dataset.idx, e.detail.majorIndex);
//     const index = e.currentTarget.dataset.idx;
//     const value = e.detail.majorIndex;
//     this.setData({
//       [`target[${index}].majorIndex`]: value
//     });
//   },
//   onRemoveTarget(e) {
//     console.log('Remove target in branch', e.currentTarget.dataset.idx);
//     const index = e.currentTarget.dataset.idx;
//     this.data.target.splice(index, 1);
//     this.setData({
//       target: this.data.target
//     });
//   }
// });
import * as schoolApi from '../../api/school';
Page({
  data: {
    schoolOptions: [],
    selectedSchools: [],
    totalUserCount: 0,
    fee: 0
  },
  onLoad() {
    this.fetchSchoolOptions();
  },
  async fetchSchoolOptions() {
    const schoolOptions = await schoolApi.getSchoolRegisterCount();
    this.setData({
      schoolOptions: schoolOptions.data
    });
  },
  onSchoolSelectChange(e) {
    this.setData({
      selectedSchools: e.detail.value
    });
    this.updateTotalUserCount();
    this.updateFee();
  },
  updateTotalUserCount() {
    const totalUserCount = this.data.selectedSchools.reduce((total, index) => {
      return total + (this.data.schoolOptions[index]?.userCount || 0);
    }, 0);
    this.setData({
      totalUserCount
    });
  },
  updateFee() {
    const feePerUser = 0.1;
    const fee = this.data.totalUserCount * feePerUser;
    this.setData({
      fee: fee.toFixed(2)
    });
  },
  onSubmit() {
    wx.showToast({
      title: 'Submitted',
      icon: 'success'
    });
  }
});
